﻿#region Copyright (c) 2008 plexBB Team
/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
#endregion

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PlexBB.Controls.Admin
{
    public delegate void ConfirmEventHandler(object sender, ConfirmEventArgs e);
    public abstract class Confirm : ForumControl
    {
        public event ConfirmEventHandler YesClick;
        public event ConfirmEventHandler NoClick;

        #region Controls
        protected ITextControl tMessageTitle;
        protected ITextControl tMessageText;
        protected IButtonControl bYes;
        protected IButtonControl bNo;
        #endregion

        public string MessageTitle
        {
            get { return tMessageTitle.Text; }
            set { tMessageTitle.Text = value; }
        }

        public string MessageText
        {
            get { return tMessageText.Text; }
            set { tMessageText.Text = value; }
        }

        public string ConfirmCommand
        {
            get { return (string)ViewState["ConfirmCommand"]; }
            set { ViewState["ConfirmCommand"] = value; }
        }

        public object ConfirmArgument
        {
            get { return ViewState["ConfirmArgument"]; }
            set { ViewState["ConfirmArgument"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            bYes.Click += bYes_Click;
            bNo.Click += bNo_Click;
        }

        protected virtual void OnYesClick(ConfirmEventArgs e)
        {
            if (YesClick != null)
            {
                YesClick(this, e);
            }
        }

        protected virtual void OnNoClick(ConfirmEventArgs e)
        {
            if (NoClick != null)
            {
                NoClick(this, e);
            }
        }

        private void bNo_Click(object sender, EventArgs e)
        {
            OnNoClick(new ConfirmEventArgs(ConfirmCommand, ConfirmArgument));
        }

        private void bYes_Click(object sender, EventArgs e)
        {
            OnYesClick(new ConfirmEventArgs(ConfirmCommand, ConfirmArgument));
        }
    }

    public class ConfirmEventArgs : EventArgs
    {
        private readonly string _confirmCommand;
        private readonly object _confirmArgument;

        public ConfirmEventArgs(string confirmCommand, object confirmArgument)
        {
            _confirmCommand = confirmCommand;
            _confirmArgument = confirmArgument;
        }

        public string ConfirmCommand
        {
            get { return _confirmCommand; }
        }

        public object ConfirmArgument
        {
            get { return _confirmArgument; }
        }
    }
}
